package com.ygu.mylist.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.ygu.mylist.R;
import com.ygu.mylist.db.DBQueryImpl;
import com.ygu.mylist.db.DBQueryResult;
import com.ygu.mylist.db.DataBaseHelper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.IBinder;
import android.util.Log;

public class RemindService extends Service{
	private NotificationManager mNotificationManager;
	private Notification mNotification;
	private SQLiteOpenHelper dbh;
	private String current_time;
	private List<DBQueryResult> list_data;
	private SimpleDateFormat sdf;
	private Calendar calendar;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressLint("SimpleDateFormat")
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		mNotificationManager=(NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
		dbh=new DataBaseHelper(getApplicationContext());
		list_data=new DBQueryImpl(dbh.getWritableDatabase()).query_current_list();
		sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		calendar=Calendar.getInstance();
		current_time=sdf.format(System.currentTimeMillis());
		mNotification=new Notification.Builder(getApplicationContext())
		.setContentText("你有任务")
		.build();
		Timer mtimer=new Timer();
		mtimer.schedule(mtask, 1000, 1000);
		super.onCreate();
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}
	TimerTask mtask=new TimerTask() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			
			for(int i=0;i<list_data.size();i++){
				if(current_time.equals(list_data.get(i).getRemind_time())){
					Log.i("有没有执行", "执行没？");
					mNotificationManager.notify(1, mNotification);
				}else{
					return;
				}
			}
		}
		
	};

}
